package com.compile;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.util.UUID;

/**
 * 文件处理工具类
 */

public class Tools {

    public static final int INSERT_DIRECTION_BEFORE = 0;
    public static final int INSERT_DIRECTION_AFTER = 1;


    public static String createFile(String fileName, String filecontent, String path) {
        boolean bool = false;
        String filenameTemp = path + fileName + ".java";

        File fileDir = new File(path);
        try {
            //如果文件不存在，则创建新的文件
            if (!fileDir.exists()) {
                fileDir.mkdirs();
            }
            File file = new File(filenameTemp);
            if (file.exists()) {
                file.setWritable(true);
                file.setExecutable(true);
                file.setReadable(true);
                file.delete();
            }
            if (!file.exists()) {
                file.createNewFile();
                bool = true;
                System.out.println("success create file,the file is " + filenameTemp);
                //创建文件成功后，写入内容到文件里
                writeFileContent(filenameTemp, filecontent, "}", INSERT_DIRECTION_BEFORE);
            }

        } catch (Exception e) {
            e.printStackTrace();
        }
        if (bool) {
            return filenameTemp;
        } else {
            return null;
        }

    }

    /**
     * 向文件中写入内容
     *
     * @param filepath 文件路径与名称
     * @param newstr   写入的内容
     * @return
     * @throws IOException
     */
    public static boolean writeFileContent(String filepath, String newstr, String indexStr, int direction) throws IOException {
        Boolean bool = false;
        String filein = newstr;//新写入的行，换行
        String temp = "";

        FileInputStream fis = null;
        InputStreamReader isr = null;
        BufferedReader br = null;
        FileOutputStream fos = null;
        PrintWriter pw = null;
        try {
            File file = new File(filepath);//文件路径(包括文件名称)
            //将文件读入输入流
            fis = new FileInputStream(file);
            isr = new InputStreamReader(fis);
            br = new BufferedReader(isr);
            StringBuffer buffer = new StringBuffer();

            //文件原有内容
            for (int i = 0; (temp = br.readLine()) != null; i++) {
                buffer.append(temp);
                // 行与行之间的分隔符 相当于“\n”
                buffer = buffer.append(System.getProperty("line.separator"));
            }

            int index = buffer.lastIndexOf(indexStr);
            if (index != -1) {
                if (INSERT_DIRECTION_BEFORE == direction) {
                    buffer.insert(index, filein);
                } else if (INSERT_DIRECTION_AFTER == direction) {
                    if (indexStr != null && !"".equals(indexStr)) {
                        buffer.insert(index + indexStr.length(), filein);
                    }

                }

            } else {
                buffer.append(filein);
            }


            fos = new FileOutputStream(file);
            pw = new PrintWriter(fos);
            pw.write(buffer.toString().toCharArray());
            pw.flush();
            bool = true;
        } catch (Exception e) {
            // TODO: handle exception
            e.printStackTrace();
        } finally {
            //不要忘记关闭
            if (pw != null) {
                pw.close();
            }
            if (fos != null) {
                fos.close();
            }
            if (br != null) {
                br.close();
            }
            if (isr != null) {
                isr.close();
            }
            if (fis != null) {
                fis.close();
            }
        }
        return bool;
    }


}
